import { ElMessage, ElMessageBox } from "element-plus";
import { createRouter, createWebHistory } from "vue-router";

const routes = [
  {
    path: "/",
    component: () => import("@/views/home.vue"),
    redirect: "/userhome",
    children: [
      {
        path: "/userhome",
        component: () => import("@/views/userpage/userhome.vue"),
      },
      {
        path: "/news",
        component: () => import("@/views/userpage/news.vue"),
      },
      {
        path: "/succnews",
        component: () => import("@/views/userpage/succnews.vue"),
      },
      {
        path: "/active",
        component: () => import("@/views/userpage/active.vue"),
        redirect: '/active/list',
        children:[
          {
            path:'/active/list',
            name:'List',
            component:() => import('@/views/userpage/ActiveList.vue')
          },
          {
            path:'/active/contents',
            name:'Cont',
            component:() => import('@/views/userpage/ActiveCont.vue')
          }
        ]
      },
      {
        path: "/reviews",
        component: () => import("@/views/userpage/reviews.vue"),
      },
      {
        path: "/aboutme",
        component: () => import("@/views/userpage/aboutme.vue"),
        beforeEnter: (to: any, from: any, next: () => any) => {
          const userInfos = window.sessionStorage.getItem("Infos");
          if (userInfos == null) {
            ElMessageBox.alert("请先登录", {
              confirmButtonText: "OK",
              callback: () => {
                router.push("/ulogin");
                window.sessionStorage.clear();
              },
            });
          } else {
            next();
          }
        },
      },
      {
        path: "/LoserDetail",
        component: () => import("@/views/userpage/LoserDetail.vue"),
      },
      {
        path: "/LoserInfos",
        component: () => import("@/views/userpage/LoserInfos.vue"),
      },
      {
        path: "/center",
        component: () => import("@/views/userpage/owner/center.vue"),
        redirect: "/center/info",
        children: [
          {
            path: "/center/info",
            component: () => import("@/views/userCenter/info.vue"),
          },
          {
            path: "/center/image",
            component: () => import("@/views/userCenter/image.vue"),
          },
          {
            path: "/center/password",
            component: () => import("@/views/userCenter/password.vue"),
          },
          {
            path: "/center/comment",
            component: () => import("@/views/userCenter/comment.vue"),
          },
          {
            path: "/center/loserInfo",
            component: () => import("@/views/userCenter/loserInfo.vue"),
          },
          {
            path: "/center/volReg",
            component: () => import("@/views/userCenter/volReg.vue"),
          },
        ],
      },
      {
        path: "/registe",
        component: () => import("@/views/userpage/owner/registe.vue"),
      },
    ],
  },
  {
    path: "/ulogin",
    component: () => import("@/components/ulogin.vue"),
  },
  {
    path: "/alogin",
    component: () => import("@/components/alogin.vue"),
  },
  {
    path: "/admin",
    component: () => import("@/views/adminpage/adminhome.vue"),
    redirect: "/admin/Welcome",
    children: [
      {
        path: "/admin/Welcome",
        component: () => import("@/views/Welcome.vue"),
      },
      {
        path: "/admin/User",
        component: () => import("@/views/adminpage/User.vue"),
      },
      {
        path: "/admin/Loser",
        component: () => import("@/views/adminpage/Loser.vue"),
      },
      {
        path: "/admin/Volunteer",
        component: () => import("@/views/adminpage/Volunteer.vue"),
      },
      {
        path: "/admin/Volactive",
        component: () => import("@/views/adminpage/Volactive.vue"),
      },
      {
        path: "/admin/Succnew",
        component: () => import("@/views/adminpage/Succnew.vue"),
      },
      // {
      //   path: "/admin/RelateNews",
      //   component: () => import("@/views/adminpage/relateNews.vue"),
      // },
      // {
      //   path: "/admin/Video",
      //   component: () => import("@/views/adminpage/video.vue"),
      // },
      {
        path: "/admin/Reviews",
        component: () => import("@/views/adminpage/Reviews.vue"),
      },
      {
        path: "/admin/adminCenter",
        component: () => import("@/views/adminpage/adminCenter.vue"),
      },
      {
        path: "/admin/MyInfos",
        component: () => import("@/views/adminpage/MyInfos.vue"),
      },
    ],
  },
  {
    path: "/rlogin",
    component: () => import("@/components/rlogin.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

//路由导航前置守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/alogin" || to.path === "/ulogin") return next();
  const adminInfos = window.sessionStorage.getItem("adminInfos");
  if (
    to.path === "/admin/Welcome" ||
    to.path === "/admin/User" ||
    to.path === "/admin/Loser" ||
    to.path === "/admin/Linkmans" ||
    to.path === "/admin/Volunteer" ||
    to.path === "/admin/Volactive" ||
    to.path === "/admin/Succnew" ||
    to.path === "/admin/Reviews"
  ) {
    if (adminInfos != null) {
      next();
    } else {
      next("/alogin");
    }
  } else {
    next();
  }
});
export default router;
